package com.ly.aidlpratice.local

import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import com.ly.aidlpratice.util.logDebug

class LocalService : Service() {

    private val mTag: String by lazy { this::class.java.simpleName }

    private val mLocalBinder = LocalBinder();

    override fun onCreate() {
        super.onCreate()
        logDebug(mTag, "onCreate")
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        logDebug(mTag, "onStartCommand")
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        super.onDestroy()
        logDebug(mTag, "onDestroy")
    }

    override fun onBind(intent: Intent): IBinder {
        logDebug(mTag, "onBind $intent")
        return mLocalBinder
    }

    override fun onUnbind(intent: Intent?): Boolean {
        logDebug(mTag, "onUnbind ${intent?.toString() ?: ""}")
        return super.onUnbind(intent)
    }

    fun getRandomNumber(): String {
        logDebug(mTag, "getRandomNumber")
        return Math.random().toString()
    }

    inner class LocalBinder : Binder() {
        fun getLocalService(): LocalService {
            return this@LocalService
        }
    }
}